import { createFileRoute } from '@tanstack/react-router'
import Users from '@/features/users'
import { User } from '@/features/users/data/schema'
import { userKeys, users } from '@/api/users'
import { dictGet } from '@/api/dicts'
import { DictItem } from '@/features/dicts/data/schema'

export const Route = createFileRoute('/_authenticated/users/')({
  component: Users,
  loader: async ({ context: { queryClient } }): Promise<{ users: User[], sexDict: DictItem[] }> => {
    const res = await queryClient.fetchQuery({
      queryKey: userKeys.all(),
      queryFn: users
    })

    const sexDictRes = await queryClient.fetchQuery({
      queryKey: ['dicts', 'sex'],
      queryFn: () => dictGet('sex'),
    })

    return { users: res.data.data, sexDict: sexDictRes.data.data };
  }
})
